// //表格初始化
// $(document).ready(function () {
//     $('#group_info_table').DataTable({
//         // fixedHeader: true
//     });
// });
var myChart11 = echarts.init(document.getElementById('main_yq'));
myChart11.showLoading();
// 点击搜索按钮的事件
$("#drawPath").on('click', function (event) {

    var data = { phone_num: $("#inputPassword").val()}
    console.log(data);
    myChart11.showLoading();
    //请求
    $.post("http://127.0.0.1:9998/analysis/get_user_group", data, (response) => {

        const json = response.data;
        console.log(json);
        myChart11.hideLoading();
        var graph = json.nodesAndLinks;
        // $.cookie("group_id", "15761")
        graph.nodes.forEach(function (node) {
            node.itemStyle = null;
            node.symbolSize = node.value * 50;
            node.label = {
                normal: {
                    show: node.value > 1
                }
            };
        });
        option = {
            tooltip: {
                position: 'right',
                formatter: function (d) {
                    console.log(d.data);
                    var data = d.data.attr
                    str = "用户电话 :" + data['phoneNum'] + "<br>"
                        + "城市 :" + data['city'] + "<br>"
                        + "关键字 :" + data['keyword'] + "<br>"
                        + "影响力 :" + data['influence'] + "<br>"
                        + "用户标签 :" + data['userLabel'] + "<br>"
                    // for(var item in data){
                    //     str = str + item + ":" + data[item] + "<br>"
                    // }
                    return str
                }
            },
            legend: {
                data: graph.categories.map(function (a) {
                    return a.name;
                }),
            },

            series: [
                {
                    type: 'graph',
                    // layout: 'circular',//环形视图
                    layout: 'force',//力导向视图（数据大时不建议）
                    force: {
                        initLayout: 0,
                        repulsion: 100,//节点之间的斥力因子。支持数组表达斥力范围，值越大斥力越大。
                        gravity: 0.03,//节点受到的向中心的引力因子。该值越大节点越往中心点靠拢。
                        edgeLength: 80,//边的两个节点之间的距离，这个距离也会受 repulsion。[10, 50] 。值越小则长度越长
                        layoutAnimation: true
                    },
                    circular: {
                        rotateLabel: true
                    },
                    data: graph.nodes,
                    links: graph.links,
                    categories: graph.categories,
                    roam: true,
                    label: {
                        normal: {
                            position: 'right',
                            formatter: '用户{b}'
                        }
                    },
                    lineStyle: {
                        normal: {
                            color: 'target',
                            curveness: 0.3
                        }
                    },
                    // style:{'diamond'},
                }
            ]
        };
        myChart11.setOption(option);
        //-----------------------------------右侧表格部分---------------------------------------------
        $("#group_info_table").dataTable().fnDestroy();//还原初始化
        let i = 1;//序号控制变量
        $('#group_info_table').DataTable({
            "bSearching": false,
            "lengthMenu": [5, 15, 25],
            "bStateSave": true,//翻页时保存状态
            "ordering": false,//禁用全局排序
            "sort": "position",
            data: json.data,
            //设置列数据
            columns: [
                {
                    "data": null,
                    "width": "9%",
                    "render": function (data, type, row, meta) {
                        return data = '<p style="text-align: center;font-size: 15px;">' + (i++) + '</p >'
                    }
                },
                {
                    "data": 'id',
                    "width": "9%",
                    "render": function (data, type, row, meta) {
                        return data = '<p style="text-align: center;font-size: 15px;">' + data + '</p >'
                    }
                },
                {
                    "data": 'groupType',
                    "width": "10%",
                    "render": function (data, type, row, meta) {
                        return data = '<p style="text-align: center;font-size: 15px;">' + data + '</p >'
                    }
                },
                {
                    "data": 'groupLeader',
                    "width": "15%",
                    "render": function (data, type, row, meta) {
                        return data = '<p style="text-align: center;font-size: 15px;">' + data + '</p >'
                    }
                },
                {
                    "data": 'groupKeyword',
                    "width": "55%",
                    "render": function (data, type, row, meta) {
                        return data = '<p style="font-size: 15px;">' + data + '</p >'
                    }
                },
                {
                    "data": 'id',
                    "width": "11%",
                    "render": function (data, type, row, meta) {
                        return data = '<button style="margin:0 auto;" class="btn btn-success" id="switch_gruop_info" onclick="Go_group_info(' + data + ',' + '\'' + row.groupType + '\'' + ')">查看群体信息</button>'
                    }
                },
            ],
            //设置排序
            "order": [[1, 'asc']],
            //设置语言
            language: {
                "sProcessing": "处理中...",
                "sLengthMenu": "显示 _MENU_ 项搜索结果",
                "sZeroRecords": "没有匹配结果",
                "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 条数据",
                "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
                "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
                "sInfoPostFix": "",
                "sSearch": "搜索:",
                "sUrl": "",
                "sEmptyTable": "未搜索到数据",
                "sLoadingRecords": "载入中...",
                "sInfoThousands": ",",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "上页",
                    "sNext": "下页",
                    "sLast": "末页"
                },
                "oAria": {
                    "sSortAscending": ": 以升序排列此列",
                    "sSortDescending": ": 以降序排列此列"
                }
            },
        });
    }, 'json');

});

//点击Echarts图中的用户节点，跳转到key_user界面
window.onresize = function () {         //自适应屏幕
    myChart11.resize()
}
//点击Echarts图节点跳转的事件
myChart11.on('click', function (params) {
    let clickPhone = params.data.attr.phoneNum;
    //设置指定key的数据（JSON格式）,在缓存中将电话号码设置为“clickPhone”，ciyun.js和gr.js可以调用缓存中的clickPhone
    // window.localStorage.setItem("clickPhone", clickPhone);
    $.cookie("node_phone_num", clickPhone)
    //将用户类型type_user设置为1，在gr.js判断type_user的值
    // document.getElementById("type_user_l").innerHTML='1';
    creatIframe('/key_user', '用户信息');
})
//默认加载的Echarts图
var data = {phone_num: 13250066978}
myChart11.showLoading();
//请求
$.post("http://127.0.0.1:9998/analysis/get_user_group", data, (response) => {

    const json = response.data;
    console.log(json);
    myChart11.hideLoading();
    var graph = json.nodesAndLinks;
    // $.cookie("group_id", "15761")
    graph.nodes.forEach(function (node) {
        node.itemStyle = null;
        node.symbolSize = node.value * 50;
        node.label = {
            normal: {
                show: node.value > 1
            }
        };
    });
    option = {
        tooltip: {
            position: 'right',
            formatter: function (d) {
                console.log(d.data);
                var data = d.data.attr
                str = "用户电话 :" + data['phoneNum'] + "<br>"
                    + "城市 :" + data['city'] + "<br>"
                    + "关键字 :" + data['keyword'] + "<br>"
                    + "影响力 :" + data['influence'] + "<br>"
                    + "用户标签 :" + data['userLabel'] + "<br>"
                // for(var item in data){
                //     str = str + item + ":" + data[item] + "<br>"
                // }
                return str
            }
        },
        legend: {
            data: graph.categories.map(function (a) {
                return a.name;
            }),
        },

        series: [
            {
                type: 'graph',
                // layout: 'circular',//环形视图
                layout: 'force',//力导向视图（数据大时不建议）
                force: {
                    initLayout: 0,
                    repulsion: 100,//节点之间的斥力因子。支持数组表达斥力范围，值越大斥力越大。
                    gravity: 0.03,//节点受到的向中心的引力因子。该值越大节点越往中心点靠拢。
                    edgeLength: 80,//边的两个节点之间的距离，这个距离也会受 repulsion。[10, 50] 。值越小则长度越长
                    layoutAnimation: true
                },
                circular: {
                    rotateLabel: true
                },
                data: graph.nodes,
                links: graph.links,
                categories: graph.categories,
                roam: true,
                label: {
                    normal: {
                        position: 'right',
                        formatter: '用户{b}'
                    }
                },
                lineStyle: {
                    normal: {
                        color: 'target',
                        curveness: 0.3
                    }
                },
                // style:{'diamond'},
            }
        ]
    };
    myChart11.setOption(option);
    //-----------------------------------右侧表格部分---------------------------------------------
    $("#group_info_table").dataTable().fnDestroy();//还原初始化
    let i = 1;//序号控制变量
    $('#group_info_table').DataTable({
        "bSearching": false,
        "lengthMenu": [5, 15, 25],
        "bStateSave": true,//翻页时保存状态
        "ordering": false,//禁用全局排序
        "sort": "position",
        data: json.data,
        //设置列数据
        columns: [
            {
                "data": null,
                "width": "9%",
                "render": function (data, type, row, meta) {
                    return data = '<p style="text-align: center;font-size: 15px;">' + (i++) + '</p >'
                }
            },
            {
                "data": 'id',
                "width": "9%",
                "render": function (data, type, row, meta) {
                    return data = '<p style="text-align: center;font-size: 15px;">' + data + '</p >'
                }
            },
            {
                "data": 'groupType',
                "width": "10%",
                "render": function (data, type, row, meta) {
                    return data = '<p style="text-align: center;font-size: 15px;">' + data + '</p >'
                }
            },
            {
                "data": 'groupLeader',
                "width": "15%",
                "render": function (data, type, row, meta) {
                    return data = '<p style="text-align: center;font-size: 15px;">' + data + '</p >'
                }
            },
            {
                "data": 'groupKeyword',
                "width": "55%",
                "render": function (data, type, row, meta) {
                    return data = '<p style="font-size: 15px;">' + data + '</p >'
                }
            },
            {
                "data": 'id',
                "width": "11%",
                "render": function (data, type, row, meta) {
                    return data = '<button style="margin:0 auto;" class="btn btn-success" id="switch_gruop_info" onclick="Go_group_info(' + data + ',' + '\'' + row.groupType + '\'' + ')">查看群体信息</button>'
                }
            },
        ],
        //设置排序
        "order": [[1, 'asc']],
        //设置语言
        language: {
            "sProcessing": "处理中...",
            "sLengthMenu": "显示 _MENU_ 项搜索结果",
            "sZeroRecords": "没有匹配结果",
            "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 条数据",
            "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
            "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
            "sInfoPostFix": "",
            "sSearch": "搜索:",
            "sUrl": "",
            "sEmptyTable": "未搜索到数据",
            "sLoadingRecords": "载入中...",
            "sInfoThousands": ",",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "上页",
                "sNext": "下页",
                "sLast": "末页"
            },
            "oAria": {
                "sSortAscending": ": 以升序排列此列",
                "sSortDescending": ": 以降序排列此列"
            }
        },
    });
}, 'json');

//跳转方法
function Go_group_info(group_id, group_type) {

    $.cookie("group_id", group_id)
    console.log(group_id)
    console.log(group_type)
    $.cookie("group_type",group_type)
    creatIframe('/group_info', group_type+'群体信息');
}